<?php
/**
 * @title 36. 有效的数独 - 哈希
 * @author start2004
 */

class Solution {

    /**
     * @param String[][] $board
     * @return Boolean
     */
    function isValidSudoku($board) {
        /**
         * @since 2021-09-18 行数组, 列数组, 区域数组
         */
        $rowArray = $colArray = $areaArray = [];

        /**
         * @since 2021-09-18 board对应区域
         */
        $boardArea = [
            [0,0,0,1,1,1,2,2,2],
            [0,0,0,1,1,1,2,2,2],
            [0,0,0,1,1,1,2,2,2],

            [3,3,3,4,4,4,5,5,5],
            [3,3,3,4,4,4,5,5,5],
            [3,3,3,4,4,4,5,5,5],

            [6,6,6,7,7,7,8,8,8],
            [6,6,6,7,7,7,8,8,8],
            [6,6,6,7,7,7,8,8,8],
        ];

        /**
         * @since 2021-09-18 有效数独的判断
         */
        for ($row=0; $row<=8; $row++){
            for($col=0; $col<=8; $col++){
                /**
                 * @since 2021-09-18 数字
                 */
                $num = $board[$row][$col];

                /**
                 * @since 2021-09-18 空格
                 */
                if($num == "."){
                    continue;
                } else {}

                /**
                 * @since 2021-09-18 区域
                 */
                // $area = intval($row/3)*3 + intval($col/3);
                $area = $boardArea[$row][$col];

                /**
                 * @since 2021-09-18 行, 列, 区域均不能再出现
                 */
                if(isset($rowArray[$row][$num]) OR isset($colArray[$col][$num]) OR isset($areaArray[$area][$num])){
                    return false;
                } else {
                    $rowArray[$row][$num] = $colArray[$col][$num] = $areaArray[$area][$num] = 1;
                }
            }
        }

        /**
         * @return
         */
        return true;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.15_10.php
 */
$datas = [
    [
        ["8","3",".",  ".","7",".",  ".",".",".",],
        ["6",".",".",  "1","9","5",  ".",".",".",],
        [".","9","8",  ".",".",".",  ".","6",".",],

        ["8",".",".",  ".","6",".",  ".",".","3",],
        ["4",".",".",  "8",".","3",  ".",".","1",],
        ["7",".",".",  ".","2",".",  ".",".","6",],

        [".","6",".",  ".",".",".",  "2","8",".",],
        [".",".",".",  "4","1","9",  ".",".","5",],
        [".",".",".",  ".","8",".",  ".","7","9",],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->isValidSudoku($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
